//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package issue_dao

import (
	"context"
	"time"

	"atomgit.com/openlinksaas-org/api-server/dao/db_ns"
	"atomgit.com/openlinksaas-org/proto-gen-go.git/project_issue_api"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

type BasicSubIssueInfoDbItem struct {
	Title string `bson:"title"`
}

type SubIssueInfoDbItem struct {
	SubIssueId   string                  `bson:"_id"`
	ProjectId    string                  `bson:"projectId"`
	IssueId      string                  `bson:"issueId"`
	BasicInfo    BasicSubIssueInfoDbItem `bson:"basicInfo"`
	CreateUserId string                  `bson:"createUserId"`
	CreateTime   int64                   `bson:"createTime"`
	UpdateTime   int64                   `bson:"updateTime"`
	Done         bool                    `bson:"done"`
}

func (item *SubIssueInfoDbItem) ToSubIssueInfo() *project_issue_api.SubIssueInfo {
	return &project_issue_api.SubIssueInfo{
		SubIssueId: item.SubIssueId,
		ProjectId:  item.ProjectId,
		IssueId:    item.IssueId,
		BasicInfo: &project_issue_api.BasicSubIssueInfo{
			Title: item.BasicInfo.Title,
		},
		CreateUserId: item.CreateUserId,
		CreateTime:   item.CreateTime,
		UpdateTime:   item.UpdateTime,
		Done:         item.Done,
	}
}

func (item *SubIssueInfoDbItem) ToSimpleSubIssue() *project_issue_api.SimpleSubIssue {
	return &project_issue_api.SimpleSubIssue{
		SubIssueId: item.SubIssueId,
		Title:      item.BasicInfo.Title,
		Done:       item.Done,
		IssueId:    item.IssueId,
	}
}

type _SubIssueDao struct {
	mc *mongo.Collection
}

func newSubIssueDao(db *mongo.Database) (*_SubIssueDao, error) {
	mc := db.Collection(db_ns.DB_NS_ISSUE + "sub_info")
	_, err := mc.Indexes().CreateOne(context.Background(), mongo.IndexModel{
		Keys: bson.D{{"issueId", 1}},
	})
	if err != nil {
		return nil, err
	}
	return &_SubIssueDao{mc: mc}, nil
}

func (db *_SubIssueDao) Insert(ctx context.Context, dbItem *SubIssueInfoDbItem) error {
	_, err := db.mc.InsertOne(ctx, dbItem)
	return err
}

func (db *_SubIssueDao) UpdateBasicInfo(ctx context.Context, projectId, issueId, subIssueId string, dbItem *BasicSubIssueInfoDbItem) error {
	_, err := db.mc.UpdateOne(ctx, bson.M{"_id": subIssueId, "projectId": projectId, "issueId": issueId},
		bson.M{"$set": bson.M{"basicInfo": dbItem, "updateTime": time.Now().UnixNano() / 1e6}})
	return err
}

func (db *_SubIssueDao) UpdateDoneFlag(ctx context.Context, projectId, issueId, subIssueId string, done bool) error {
	_, err := db.mc.UpdateOne(ctx, bson.M{"_id": subIssueId, "projectId": projectId, "issueId": issueId},
		bson.M{"$set": bson.M{"done": done, "updateTime": time.Now().UnixNano() / 1e6}})
	return err
}

func (db *_SubIssueDao) ListByIssue(ctx context.Context, projectId string, issueIdList []string) ([]*SubIssueInfoDbItem, error) {
	query := bson.M{"issueId": bson.M{"$in": issueIdList}}
	if projectId != "" {
		query["projectId"] = projectId
	}
	cursor, err := db.mc.Find(ctx, query, options.Find().SetSort(bson.M{"createTime": 1}))
	if err != nil {
		return nil, err
	}
	retList := []*SubIssueInfoDbItem{}
	err = cursor.All(ctx, &retList)
	return retList, err
}

func (db *_SubIssueDao) Remove(ctx context.Context, projectId, issueId, subIssueId string) error {
	_, err := db.mc.DeleteOne(ctx, bson.M{"_id": subIssueId, "projectId": projectId, "issueId": issueId})
	return err
}

func (db *_SubIssueDao) RemoveByIssue(ctx context.Context, projectId, issueId string) error {
	_, err := db.mc.DeleteMany(ctx, bson.M{"projectId": projectId, "issueId": issueId})
	return err
}

func (db *_SubIssueDao) Get(ctx context.Context, projectId, issueId, subIssueId string) (*SubIssueInfoDbItem, error) {
	retItem := &SubIssueInfoDbItem{}
	err := db.mc.FindOne(ctx, bson.M{"_id": subIssueId, "projectId": projectId, "issueId": issueId}).Decode(retItem)
	return retItem, err
}
